package com.kaisa.activity.ui;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.adapter.TravelGridAdapter;
import com.kaisa.activity.adapter.WelcomeAdapter;
import com.kaisa.activity.bean.TravelCardBean;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class TravelActivity extends AppCompatActivity {
    @ViewInject(R.id.et_travel_name)
    private EditText name;
    @ViewInject(R.id.et_travel_password)
    private EditText password;
    @ViewInject(R.id.vp_travel)
    private ViewPager travelVP;
    @ViewInject(R.id.gv_travel)
    private GridView gridView;

    @ViewInject(R.id.ll_travel_dots)
    private LinearLayout linearLayout;

    private WelcomeAdapter adapterVP;

    private int[] imgsTop = {R.mipmap.tour_card_vp1,R.mipmap.tour_card_vp2,
            R.mipmap.tour_card_vp3,R.mipmap.tour_card_vp4,};
    private List<ImageView> listImgTop = new ArrayList<ImageView>();

    private String[] titles = {"世嘉A卡","世嘉卡","礼品卡"};
    private String[] contens = {"预团费 让团费生钱","风景是最美的礼物","缤纷主题"};
    private int[] imgsGrid = {R.mipmap.sega_gold_card,R.mipmap.sega_card,R.mipmap.gift_card};
    private List<TravelCardBean> listGrid = new ArrayList<TravelCardBean>();

    private TravelGridAdapter adapterGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel);
        x.view().inject(this);

        initView();
        initData();
        addListener();
    }



    private void initView() {
        adapterVP = new WelcomeAdapter(listImgTop);
        travelVP.setAdapter(adapterVP);

        adapterGrid = new TravelGridAdapter(listGrid,this);
        gridView.setAdapter(adapterGrid);
    }

    private void initData() {
        for (int i = 0; i < imgsTop.length; i++) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(imgsTop[i]);
            listImgTop.add(iv);
        }
        for (int i = 0; i < imgsTop.length; i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            ImageView ivDot = new ImageView(this);
            ivDot.setLayoutParams(params);
            ivDot.setScaleType(ImageView.ScaleType.FIT_XY);
            ivDot.setImageResource(R.drawable.bg_dots);
            linearLayout.addView(ivDot);
        }
        linearLayout.getChildAt(0).setEnabled(false);
        adapterVP.notifyDataSetChanged();

        for (int i = 0; i < imgsGrid.length; i++) {
            TravelCardBean bean = new TravelCardBean(titles[i],contens[i],imgsGrid[i]);
            listGrid.add(bean);
        }
        adapterGrid.notifyDataSetChanged();
    }

    private void addListener() {
        travelVP.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imgsTop.length; i++) {
                    linearLayout.getChildAt(i).setEnabled(true);
                }
                linearLayout.getChildAt(position).setEnabled(false);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void click(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                this.finish();
                break;
            case R.id.btn_login:
                String username = name.getText().toString();
                String psw = password.getText().toString();
                if (username.equals("") || password.equals("")) {
                    BaseApp.getInstance().toastShow("用户名或密码为空号");
                } else {
                    BaseApp.getInstance().toastShow("登陆失败");
                }
                break;
        }
    }

}
